package tenantchannellogic

import (
	"context"
	tenantChannel "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpiUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpiUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpiUpdateLogic {
	return &UpiUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpiUpdateLogic) UpiUpdate(in *rpc.UpiUpdateRequest) (*rpc.UpiUpdateResponse, error) {
	var CommonResp rpc.CommonResp

	updates := make(map[string]interface{})
	if in.Upi != "" {
		updates["upi"] = in.Upi
	}
	if in.MinAmount != 0 {
		updates["min_amount"] = in.MinAmount
	}
	if in.MaxAmount != 0 {
		updates["max_amount"] = in.MaxAmount
	}
	if in.DailyTimes != 0 {
		updates["daily_times"] = in.DailyTimes
	}
	if in.DailyAmount != 0 {
		updates["daily_amount"] = in.DailyAmount
	}
	if in.RemainTimes != 0 {
		updates["remain_times"] = in.RemainTimes
	}
	if in.RemainAmount != 0 {
		updates["remain_amount"] = in.RemainAmount
	}
	if in.VisionLevel != "" {
		updates["vision_level"] = in.VisionLevel
	}
	if in.IsDisplay != 0 {
		updates["is_display"] = in.IsDisplay
	}

	err := tenantChannel.NewTenantChannelUpiModel(l.ctx, l.svcCtx.Db).UpdateUpi(in.Id, updates)
	if err != nil {
		CommonResp.Code = xerrors.DBError
		CommonResp.Message = "Database operation error"
		return &rpc.UpiUpdateResponse{
			CommonResp: &CommonResp,
		}, err
	}
	CommonResp.Message = "Success"
	return &rpc.UpiUpdateResponse{
		CommonResp: &CommonResp,
	}, nil
}
